package cpa.prowon.gui;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;

import cpa.prowon.image.ImageContainer;

/**
 * The single image information window is used to edit the IPTC infomration
 * of a single image
 * @author Chris
 */
public class MultiImageInformationWindow extends ImageInformationWindow {

	// Properties
	protected ImageContainer[] _images;
	
	/**
	 * Get the number of columns for the group widget
	 * @return The number of columns
	 * @author Chris
	 */
	protected int getGroupColumnsNumber() {
		return 3;
	}
	
	/**
	 * Get the title of the image
	 * @return The actual value
	 * @author Chris
	 */
	public MultiImageInformationWindow(MainWindow w, ImageContainer[] images) {
		
		// Create the object
		super();
		
		// Set the initial values
		mainWindow = w;
		_images = images;
		
	}

	/**
	 * Get the title of the image
	 * @return The actual value
	 * @author Chris
	 */
	protected String getTitle() {
		String temp = "";
		temp = _images[0].getTitle();
		for (int i=1; i <= _images.length; i++) {
			if (!temp.equals(_images[i-1].getTitle())) {
				return "";
			}
		}
		return temp;
	}

	/**
	 * Get the gallery of the image
	 * @return The actual value
	 * @author Chris
	 */
	protected String getGallery() {
		String temp = "";
		temp = _images[0].getGallery();
		for (int i=1; i <= _images.length; i++) {
			if (!temp.equals(_images[i-1].getGallery())) {
				return "";
			}
		}
		return temp;
	}

	/**
	 * Get the description of the image
	 * @return The actual value
	 * @author Chris
	 */
	protected String getDescription() {
		String temp = "";
		temp = _images[0].getDescription();
		for (int i=1; i <= _images.length; i++) {
			if (!temp.equals(_images[i-1].getDescription())) {
				return "";
			}
		}
		return temp;
	}

	/**
	 * Get the keywords of a single image as single string
	 * @param ImageContainer The image to be asked for the keywords
	 * @return The actual value as single string
	 * @author Chris
	 */
	protected String getSingleImageKeywords(ImageContainer image) {
		String[] keywordList = image.getKeywords();
		String tempKeywords = "";
		boolean isFirst = true;
		for (int i=1; i<=keywordList.length; i++) {
			if (isFirst == true) {
				isFirst = false;
			} else {
				tempKeywords += "\r\n";
			}
			tempKeywords += keywordList[i-1];
		}
		return tempKeywords;
	}

	/**
	 * Get the supplemental categories of a single image as single string
	 * @param ImageContainer The image to be asked for the supplemental categories
	 * @return The actual value as single string
	 * @author Chris
	 */
	protected String getSingleImageSupplementalCategories(ImageContainer image) {
		String[] catList = image.getSupplementalCategories();
		String tempCats = "";
		boolean isFirst = true;
		for (int i=1; i<=catList.length; i++) {
			if (isFirst == true) {
				isFirst = false;
			} else {
				tempCats += "\r\n";
			}
			tempCats += catList[i-1];
		}
		return tempCats;
	}

	/**
	 * Get the keywords of the image
	 * @return The actual value
	 * @author Chris
	 */
	protected String getKeywords() {
		String temp = "";
		temp = getSingleImageKeywords(_images[0]);
		for (int i=1; i <= _images.length; i++) {
			if (!temp.equals(getSingleImageKeywords(_images[i-1]))) {
				return "";
			}
		}
		return temp;
	}

	/**
	 * Get the supplemental categories of the image
	 * @return The actual value
	 * @author Chris
	 */
	protected String getSupplementalCategories() {
		String temp = "";
		temp = getSingleImageSupplementalCategories(_images[0]);
		for (int i=1; i <= _images.length; i++) {
			if (!temp.equals(getSingleImageSupplementalCategories(_images[i-1]))) {
				return "";
			}
		}
		return temp;
	}

	/**
	 * Get the city of the image
	 * @return The actual value
	 * @author Chris
	 */
	protected String getCity() {
		String temp = "";
		temp = _images[0].getCity();
		for (int i=1; i <= _images.length; i++) {
			if (!temp.equals(_images[i-1].getCity())) {
				return "";
			}
		}
		return temp;
	}

	/**
	 * Get the state of the image
	 * @return The actual value
	 * @author Chris
	 */
	protected String getProvinceState() {
		String temp = "";
		temp = _images[0].getProvinceState();
		for (int i=1; i <= _images.length; i++) {
			if (!temp.equals(_images[i-1].getProvinceState())) {
				return "";
			}
		}
		return temp;
	}

	/**
	 * Get the country of the image
	 * @return The actual value
	 * @author Chris
	 */
	protected String getCountry() {
		String temp = "";
		temp = _images[0].getCountry();
		for (int i=1; i <= _images.length; i++) {
			if (!temp.equals(_images[i-1].getCountry())) {
				return "";
			}
		}
		return temp;
	}

	/**
	 * Get the category of the image
	 * @return The actual value
	 * @author Chris
	 */
	protected String getCategory() {
		String temp = "";
		temp = _images[0].getCategory();
		for (int i=1; i <= _images.length; i++) {
			if (!temp.equals(_images[i-1].getCategory())) {
				return "";
			}
		}
		return temp;
	}

	/**
	 * Get the copyright of the image
	 * @return The actual value
	 * @author Chris
	 */
	protected String getCopyright() {
		String temp = "";
		temp = _images[0].getCopyright();
		for (int i=1; i <= _images.length; i++) {
			if (!temp.equals(_images[i-1].getCopyright())) {
				return "";
			}
		}
		return temp;
	}

	/**
	 * Get the date of the image
	 * @return The actual value
	 * @author Chris
	 */
	protected String getDate() {
		String temp = "";
		temp = _images[0].getDate();
		for (int i=1; i <= _images.length; i++) {
			if (!temp.equals(_images[i-1].getDate())) {
				return "";
			}
		}
		return temp;
	}

	/**
	 * Get the originating program of the image
	 * @return The actual value
	 * @author Chris
	 */
	protected String getOriginatingProgram() {
		String temp = "";
		temp = _images[0].getOriginatingProgram();
		for (int i=1; i <= _images.length; i++) {
			if (!temp.equals(_images[i-1].getOriginatingProgram())) {
				return "";
			}
		}
		return temp;
	}

	/**
	 * Set the title of the image
	 * @param String The actual value
	 * @author Chris
	 */
	protected void setTitle(String value) {
		for (int i=1; i <= _images.length; i++) {
			_images[i-1].setTitle(value);
		}
	}

	/**
	 * Set the gallery of the image
	 * @param String The actual value
	 * @author Chris
	 */
	protected void setGallery(String value) {
		for (int i=1; i <= _images.length; i++) {
			_images[i-1].setGallery(value);
		}
	}

	/**
	 * Set the description of the image
	 * @param String The actual value
	 * @author Chris
	 */
	protected void setDescription(String value) {
		for (int i=1; i <= _images.length; i++) {
			_images[i-1].setDescription(value);
		}
	}

	/**
	 * Set the keywords of a single image by getting a single string
	 * @param ImageContainer The image where the keywords will be set
	 * @param String The actual value
	 * @author Chris
	 */
	protected void setSingleImageKeywords(ImageContainer image, String value) {
		image.setKeywords(value.split("\r\n"));
	}

	/**
	 * Set the supplemental categories of a single image by getting a single string
	 * @param ImageContainer The image where the supplemental categories will be set
	 * @param String The actual value
	 * @author Chris
	 */
	protected void setSingleImageSupplementalCategories(ImageContainer image, String value) {
		image.setSupplementalCategories(value.split("\r\n"));
	}

	/**
	 * Set the keywords of the image
	 * @param String The actual value
	 * @author Chris
	 */
	protected void setKeywords(String value) {
		for (int i=1; i <= _images.length; i++) {
			setSingleImageKeywords(_images[i-1], value);
		}
	}

	/**
	 * Set the supplemental categories of the image
	 * @param String The actual value
	 * @author Chris
	 */
	protected void setSupplementalCategories(String value) {
		for (int i=1; i <= _images.length; i++) {
			setSingleImageSupplementalCategories(_images[i-1], value);
		}
	}

	/**
	 * Set the city of the image
	 * @param String The actual value
	 * @author Chris
	 */
	protected void setCity(String value) {
		for (int i=1; i <= _images.length; i++) {
			_images[i-1].setCity(value);
		}
	}

	/**
	 * Set the state of the image
	 * @param String The actual value
	 * @author Chris
	 */
	protected void setProvinceState(String value) {
		for (int i=1; i <= _images.length; i++) {
			_images[i-1].setProvinceState(value);
		}
	}

	/**
	 * Set the country of the image
	 * @param String The actual value
	 * @author Chris
	 */
	protected void setCountry(String value) {
		for (int i=1; i <= _images.length; i++) {
			_images[i-1].setCountry(value);
		}
	}

	/**
	 * Set the category of the image
	 * @param String The actual value
	 * @author Chris
	 */
	protected void setCategory(String value) {
		for (int i=1; i <= _images.length; i++) {
			_images[i-1].setCategory(value);
		}
	}

	/**
	 * Set the copyright of the image
	 * @param String The actual value
	 * @author Chris
	 */
	protected void setCopyright(String value) {
		for (int i=1; i <= _images.length; i++) {
			_images[i-1].setCopyright(value);
		}
	}

	/**
	 * Set the date of the image
	 * @param String The actual value
	 * @author Chris
	 */
	protected void setDate(String value) {
		for (int i=1; i <= _images.length; i++) {
			_images[i-1].setDate(value);
		}
	}

	/**
	 * Set the originating program of the image
	 * @param String The actual value
	 * @author Chris
	 */
	protected void setOriginatingProgram(String value) {
		for (int i=1; i <= _images.length; i++) {
			_images[i-1].setOriginatingProgram(value);
		}
	}

	/**
	 * Save the changes
	 * @author Chris
	 */
	protected void save() {
		for (int i=1; i <= _images.length; i++) {
			if (_images[i-1].hasChanged()) {
				_images[i-1].save();
			}
		}
	}

	/**
	 * Create a checkbox
	 * @param Composite The parent where the checkbox should be put into
	 * @return The actual checkbox
	 * @author Chris
	 */
	protected Button createCheckbox(Composite parent) {
		
		// Create the checkbox
		Button checkbox = new Button(parent, SWT.CHECK);

		// Layout the checkbox
		GridData grid = new GridData();
		grid.verticalAlignment = GridData.BEGINNING;
		checkbox.setLayoutData(grid);

		// Return the checkbox		
		return checkbox;

	}

	/**
	 * Do the initialization for the checkbox and the text field
	 * @param Button The checkbox
	 * @param Text The text field
	 * @param String The value of the text field
	 * @author Chris
	 */
	protected void initializeEntryFields(Button checkbox, Text field, String value) {
		
		// Create the listener for the checkbox
		final Text entryField = field;
		SelectionListener selection_listener = new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				Button checkbox = (Button) event.widget;
				entryField.setEnabled(checkbox.getSelection());
			}
		};
		checkbox.addSelectionListener(selection_listener);
		
		// Set the initial state
		if (value.equals("")) {
			checkbox.setSelection(false);
			field.setEnabled(false);
		} else {
			checkbox.setSelection(true);
			field.setEnabled(true);
		}
		
	}
	
	/**
	 * Create the text field for the title
	 * @param Composite The parent where the text field should be put into
	 * @return The actual text entry field
	 * @author Chris
	 */
	protected Text createTitleField(Composite parent) {
		
		// Create the checkbox, text field and initialize them
		Button checkbox = createCheckbox(parent);
		Text field = super.createTitleField(parent);
		initializeEntryFields(checkbox, field, iptc_title);
		
		// Return the field
		return field;
		
	}
	
	/**
	 * Create the text field for the gallery
	 * @param Composite The parent where the text field should be put into
	 * @return The actual text entry field
	 * @author Chris
	 */
	protected Text createGalleryField(Composite parent) {
		
		// Create the checkbox, text field and initialize them
		Button checkbox = createCheckbox(parent);
		Text field = super.createGalleryField(parent);
		initializeEntryFields(checkbox, field, iptc_gallery);
		
		// Return the field
		return field;
		
	}

	/**
	 * Create the text field for the description
	 * @param Composite The parent where the text field should be put into
	 * @return The actual text entry field
	 * @author Chris
	 */
	protected Text createDescriptionField(Composite parent) {
		
		// Create the checkbox, text field and initialize them
		Button checkbox = createCheckbox(parent);
		Text field = super.createDescriptionField(parent);
		initializeEntryFields(checkbox, field, iptc_description);
		
		// Return the field
		return field;
		
	}

	/**
	 * Create the text field for the keywords
	 * @param Composite The parent where the text field should be put into
	 * @return The actual text entry field
	 * @author Chris
	 */
	protected Text createKeywordsField(Composite parent) {
		
		// Create the checkbox, text field and initialize them
		Button checkbox = createCheckbox(parent);
		Text field = super.createKeywordsField(parent);
		initializeEntryFields(checkbox, field, iptc_keywords);
		
		// Return the field
		return field;
		
	}

	/**
	 * Create the text field for the supplemental categories
	 * @param Composite The parent where the text field should be put into
	 * @return The actual text entry field
	 * @author Chris
	 */
	protected Text createSupplementalCategoriesField(Composite parent) {
		
		// Create the checkbox, text field and initialize them
		Button checkbox = createCheckbox(parent);
		Text field = super.createSupplementalCategoriesField(parent);
		initializeEntryFields(checkbox, field, iptc_supplementalcategories);
		
		// Return the field
		return field;
		
	}

	/**
	 * Create the text field for the city
	 * @param Composite The parent where the text field should be put into
	 * @return The actual text entry field
	 * @author Chris
	 */
	protected Text createCityField(Composite parent) {
		
		// Create the checkbox, text field and initialize them
		Button checkbox = createCheckbox(parent);
		Text field = super.createCityField(parent);
		initializeEntryFields(checkbox, field, iptc_city);
		
		// Return the field
		return field;
		
	}

	/**
	 * Create the text field for the state
	 * @param Composite The parent where the text field should be put into
	 * @return The actual text entry field
	 * @author Chris
	 */
	protected Text createStateField(Composite parent) {
		
		// Create the checkbox, text field and initialize them
		Button checkbox = createCheckbox(parent);
		Text field = super.createStateField(parent);
		initializeEntryFields(checkbox, field, iptc_state);
		
		// Return the field
		return field;
		
	}

	/**
	 * Create the text field for the country
	 * @param Composite The parent where the text field should be put into
	 * @return The actual text entry field
	 * @author Chris
	 */
	protected Text createCountryField(Composite parent) {
		
		// Create the checkbox, text field and initialize them
		Button checkbox = createCheckbox(parent);
		Text field = super.createCountryField(parent);
		initializeEntryFields(checkbox, field, iptc_country);
		
		// Return the field
		return field;
		
	}

	/**
	 * Create the text field for the category
	 * @param Composite The parent where the text field should be put into
	 * @return The actual text entry field
	 * @author Chris
	 */
	protected Text createCategoryField(Composite parent) {
		
		// Create the checkbox, text field and initialize them
		Button checkbox = createCheckbox(parent);
		Text field = super.createCategoryField(parent);
		initializeEntryFields(checkbox, field, iptc_category);
		
		// Return the field
		return field;
		
	}

	/**
	 * Create the text field for the date
	 * @param Composite The parent where the text field should be put into
	 * @return The actual text entry field
	 * @author Chris
	 */
	protected Text createDateField(Composite parent) {
		
		// Create the checkbox, text field and initialize them
		Button checkbox = createCheckbox(parent);
		Text field = super.createDateField(parent);
		initializeEntryFields(checkbox, field, iptc_date);
		
		// Return the field
		return field;
		
	}

	/**
	 * Create the text field for the copyright
	 * @param Composite The parent where the text field should be put into
	 * @return The actual text entry field
	 * @author Chris
	 */
	protected Text createCopyrightField(Composite parent) {
		
		// Create the checkbox, text field and initialize them
		Button checkbox = createCheckbox(parent);
		Text field = super.createCopyrightField(parent);
		initializeEntryFields(checkbox, field, iptc_copyright);
		
		// Return the field
		return field;
		
	}

}
